#!/bin/bash

# Remote Dependency Installation Script
# This script installs required dependencies on the remote host
# Usage: ./remote-install-deps.sh <host> <user> <password>

REMOTE_HOST="${1:-192.168.10.11}"
REMOTE_USER="${2:-asr}"
REMOTE_PASSWORD="${3:-123456}"

echo "Installing dependencies on ${REMOTE_USER}@${REMOTE_HOST}..."
echo ""

# Create a temporary installation script
INSTALL_SCRIPT=$(mktemp)

cat > "$INSTALL_SCRIPT" << 'INSTALL_EOF'
#!/bin/bash
set -e

echo "=== System Information ==="
uname -a
echo ""

echo "=== Updating Package Manager ==="
sudo apt-get update -qq
echo "✓ Package manager updated"
echo ""

echo "=== Installing Build Tools ==="
sudo apt-get install -y build-essential cmake git wget curl
echo "✓ Build tools installed"
echo ""

echo "=== Installing OpenCV ==="
sudo apt-get install -y libopencv-dev python3-opencv
echo "✓ OpenCV installed"
echo ""

echo "=== Installing nlohmann_json ==="
sudo apt-get install -y nlohmann-json3-dev
echo "✓ nlohmann_json installed"
echo ""

echo "=== Installing OpenVINO ==="
sudo apt-get install -y openvino-dev
echo "✓ OpenVINO installed"
echo ""

echo "=== Verifying Installations ==="
echo ""

echo "OpenCV:"
if pkg-config --modversion opencv4 2>/dev/null; then
    echo "✓ OpenCV 4 found"
else
    echo "✗ OpenCV 4 not found"
fi

echo ""
echo "OpenVINO:"
if [ -d /opt/intel/openvino ]; then
    echo "✓ OpenVINO found at /opt/intel/openvino"
    ls -la /opt/intel/openvino/runtime/lib/intel64/ | head -3
else
    echo "✗ OpenVINO not found"
fi

echo ""
echo "nlohmann_json:"
if pkg-config --modversion nlohmann_json 2>/dev/null; then
    echo "✓ nlohmann_json found"
else
    echo "✗ nlohmann_json not found"
fi

echo ""
echo "=== Installation Complete ==="
INSTALL_EOF

# Copy script to remote host
echo "Uploading installation script..."
sshpass -p "$REMOTE_PASSWORD" scp -o StrictHostKeyChecking=no \
    "$INSTALL_SCRIPT" "${REMOTE_USER}@${REMOTE_HOST}:/tmp/install_deps.sh"

# Execute script on remote host
echo "Executing installation on remote host..."
echo ""
sshpass -p "$REMOTE_PASSWORD" ssh -o StrictHostKeyChecking=no \
    "${REMOTE_USER}@${REMOTE_HOST}" "bash /tmp/install_deps.sh"

# Cleanup
rm -f "$INSTALL_SCRIPT"

echo ""
echo "✓ Installation script completed!"

